/**
### Step 7: Create 5-year Lagged Network
**File:** `1_2_SC_Network.sas`  
**Language:** SAS  
**Description:** Creates lagged (5-year) Supply Chain Network data for both annual and quarterly frequencies.
- **Inputs:**
  - SCnetwork2_saveold 
  - SCnetwork2_Q.csv 
- **Outputs:**
  - SCnetwork2_projected5.csv
  - SCnetwork2_Q_projected5.csv

**/


libname mylib "&dir";

**** SAS CODE: ;
proc import datafile= "&dir\SCnetwork2_saveold.dta"
     out= SCnetwork2
     dbms=dta
     replace;
run;

 proc sql;
    create table SCnetwork2_projected5 as
    select distinct a.*
    from SCnetwork2 as a 
    	inner join SCnetwork2 as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+5
	;
quit; 


proc export data = SCnetwork2_projected5
			file = "&dir/SCnetwork2_projected5.csv"
			dbms = CSV REPLACE;
run;


*** SAME AS ABOVE FOR THE QUARTERLY DATASET: 

**** SAS CODE: ;
proc import datafile= "&dir\SCnetwork2_Q.csv"
     out= SCnetwork2_Q
     dbms=CSV
     replace;
run;

 proc sql;
    create table SCnetwork2_Q_projected5 as
    select distinct a.*
    from SCnetwork2_Q (drop=yearq) as a 
    	inner join SCnetwork2_Q (drop=yearq) as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+5
	;
quit; 
 proc sql;
    create table SCnetwork2_Q_projected3 as
    select distinct a.*
    from SCnetwork2_Q (drop=yearq) as a 
    	inner join SCnetwork2_Q (drop=yearq) as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+3
	;
quit; 

proc export data = SCnetwork2_Q_projected5
			file = "&dir/SCnetwork2_Q_projected5.csv"
			dbms = CSV REPLACE;
run;
proc export data = SCnetwork2_Q_projected3
			file = "&dir/SCnetwork2_Q_projected3.csv"
			dbms = CSV REPLACE;
run;


**** SAS CODE: ;
proc import datafile= "&dir\SCnetwork2_saveold.dta"
     out= SCnetwork2
     dbms=dta
     replace;
run;

 proc sql;
    create table SCnetwork2_projected5 as
    select distinct a.*
    from SCnetwork2 as a 
    	inner join SCnetwork2 as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+5
	;
quit; 


proc export data = SCnetwork2_projected5
			file = "&dir/SCnetwork2_projected5.csv"
			dbms = CSV REPLACE;
run;


*** SAME AS ABOVE FOR THE QUARTERLY DATASET: 

**** SAS CODE: ;
proc import datafile= "&dir\SCnetwork2_Q.csv"
     out= SCnetwork2_Q
     dbms=CSV
     replace;
run;

 proc sql;
    create table SCnetwork2_Q_projected5 as
    select distinct a.*
    from SCnetwork2_Q (drop=yearq) as a 
    	inner join SCnetwork2_Q (drop=yearq) as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+5
	;
quit; 
 proc sql;
    create table SCnetwork2_Q_projected3 as
    select distinct a.*
    from SCnetwork2_Q (drop=yearq) as a 
    	inner join SCnetwork2_Q (drop=yearq) as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and a.fyear=b.fyear+3
	;
quit; 

proc export data = SCnetwork2_Q_projected5
			file = "&dir/SCnetwork2_Q_projected5.csv"
			dbms = CSV REPLACE;
run;
proc export data = SCnetwork2_Q_projected3
			file = "&dir/SCnetwork2_Q_projected3.csv"
			dbms = CSV REPLACE;
run;
